package com.situ.ssh.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Repository;

import com.situ.ssh.common.ServerResponse;
import com.situ.ssh.dao.IStudentDao;
import com.situ.ssh.entity.Student;
import com.situ.ssh.util.SearchCondition;

@Repository
public class StudentDaoImpl extends BaseDaoImpl<Student> implements IStudentDao{

	@Override
	public ServerResponse pageList(SearchCondition searchCondition) {
		Integer page = searchCondition.getPage();
		Integer limit = searchCondition.getLimit();
		DetachedCriteria detachedCriteria = searchCondition.getDetachedCriteria();
		//查询total：总记录数（是带着条件查询出来的总记录数）
		detachedCriteria.setProjection(Projections.rowCount());//要查询总记录
		List<?> list = getHibernateTemplate().findByCriteria(detachedCriteria);
		long count = (long) list.get(0);
		// 得到当前页的数据
		detachedCriteria.setProjection(null);
		int firstResult = (page - 1) * limit;
		List<Student> rows = (List<Student>) getHibernateTemplate().findByCriteria(detachedCriteria, firstResult, limit);
		
		return ServerResponse.createSuccess("查找成功", (int)count, rows);
	}

}

